#import <appkit/appkit.h>

#import "JoyStick.h"

@implementation JoyStick

void JS_scroller(DPSTimedEntry teNumber, double now,id userData)
{
    NXPoint mouseLoc;

    [[userData window] getMouseLocation:&mouseLoc];
    [userData convertPoint:&mouseLoc fromView:nil];
    [userData setPos:(NXPoint)mouseLoc];

}

- (float)getYVal:sender
{
    return y*ymax;
}

- (float)getXVal:sender
{
    return x*xmax;
}

-setXMax:(float)val
{
xmax=2*val;
return self;
}
-setYMax:(float)val
{
ymax=2*val;
return self;
}

- (float)yMax
{
    return 0.5*ymax;
}
- (float)xMax
{
    return 0.5*xmax;
}

-setTrackTime:(float)val
    {
    tracktime=val;
    return self;
    }
-setFadeTime:(float)val
    {
    fadetime=val;
    return self;
    }
-(float)trackTime
    {
    return tracktime;
    }
-(float)fadeTime
    {
    return fadetime;
    }

- drawSelf:(const NXRect *)rects :(int)rectCount
{
	PSsetgray(NX_LTGRAY);
    PSrectfill(-0.55,-0.55,1.1,1.1);

    PSsetgray(.85);
    PSarc(0,0,.55,0,360);
	PSfill();   
	
	PSsethsbcolor(0,0,0);
    PSarc(0,0,.55,0,360);
	PSsetlinewidth(.005);
	PSstroke();
    
	PSsethsbcolor(1,1,0);
    PSsetlinewidth(0.005);
    PSmoveto(0,-1.1);
    PSlineto(0,1.1);
    PSmoveto(-1.1,0);
    PSlineto(1.1,0);
    PSstroke();
		
	PSsetgray(.6);
	PSmoveto(0,0);
    PSarc(0,0,.15,0,360);
	PSfill();
	
	PSsetgray(.3);
	PSmoveto(0,0);
    PSarc(0,0,.05,0,360);
	PSfill();
	
	PSnewpath();
	PSsetgray(.3);
	PSmoveto(0,0);
    PSlineto(x,y);
	PSsetlinewidth(.07);
	PSstroke();

    PSsethsbcolor(1,1,.7);
	PSarc(x, y, .15,0,360);
	PSfill();
        
    return self;
}

- initFrame:(const NXRect *)frameRect
{    
    [super initFrame:frameRect];
    
    [self setDrawSize:(NXCoord)1.1:(NXCoord)1.1];
    [self setDrawOrigin:-(float)0.55 :-(float)0.55];

    timer = (DPSTimedEntry)-1;
    tracktime=0.1;
    fadetime=0.7;
    track=NO;
    x=y=0;
    xmax=1;
    ymax=1;
    
    return self;
 }

- sizeTo:(NXCoord)width:(NXCoord)height
{
[super sizeTo:width :height];
[self setDrawSize:(NXCoord)1.1:(NXCoord)1.1];
[self setDrawOrigin:-(float)0.55 :-(float)0.55];
return self;
}

-free
{
if(timer !=  (DPSTimedEntry)-1)
	DPSRemoveTimedEntry(timer);
[super free];
return self;
}
- setPos:(NXPoint)mouseLoc
{
if(track==YES)
    {
    x=	mouseLoc.x<0.5	? mouseLoc.x 	:0.5 ;
    x=	x>-0.5			? x 				:-0.5;
    y=	mouseLoc.y<0.5	? mouseLoc.y 	: 0.5 ;
    y=	y>-0.5			? y 				:-0.5;
    }
else
    {
    x *=fadetime;
    y *=fadetime;
    if (x*x<0.0001 && y*y<0.0001 && timer != (DPSTimedEntry)-1)
	{
	DPSRemoveTimedEntry(timer);
	timer=(DPSTimedEntry) -1;
	x=y=0;
	}
    }
    [self display];
    return self;
}


- (BOOL)acceptsFirstMouse
{
    return YES;
}

- mouseDown:(NXEvent *)theEvent
{
    track=YES;
    if(timer== (DPSTimedEntry)-1)
	timer=DPSAddTimedEntry(tracktime, (DPSTimedEntryProc)JS_scroller, (void *)self, NX_BASETHRESHOLD);
    return(self);
}

- mouseUp:(NXEvent *)theEvent
{
    track=NO;
    return(self);
}

- write:(NXTypedStream *)stream
{
    [super write:stream];
    NXWriteTypes(stream,"ffff",&xmax,&ymax,&tracktime,&fadetime);
    return self;
}
    
- read:(NXTypedStream *)stream
    {
    [super read:stream];
    NXReadTypes(stream,"ffff",&xmax,&ymax,&tracktime,&fadetime);
    x=y=0;
    timer= (DPSTimedEntry)-1;
    track=NO;
    return self;
    }
    


- (const char *) getInspectorClassName
    {
    return "JoyStickInspector";
    }



@end
